﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link href="~/layui-v2.6.8/layui-v2.6.8/layui/css/layui.css" rel="stylesheet" />
    <script src="~/lib/jquery/dist/jquery.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
    <div class="demoTable">
        搜索ID：
        <div class="layui-inline">
            <input class="layui-input" name="keyName" id="demoReload" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
        <button class="layui-btn" data-type="add">添加</button>
    </div>
    <div class="layui-row">
        <div class="layui-col-xs2">
            <div id="test1" class="demo-tree demo-tree-box"></div>
        </div>
        <div class="layui-col-xs10">
            <table class="layui-hide" id="test" lay-filter="test"></table>
        </div>
    </div>


    @*<script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
            </div>
        </script>*@

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script src="~/layui-v2.6.8/layui-v2.6.8/layui/layui.js"></script>
    <script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

    <script>


        //（4） 前端：浏览器请求服务器的时候，要带上JWT
        $.ajaxSetup({
            beforeSend: function (xhr) {
                if (localStorage.getItem("token") !== null) {
                    //（4） 前端：浏览器请求服务器的时候，要带上JWT
                    xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("token"));
                }
            }
        });

        layui.use(['table', 'tree', 'util'], function () {
            var table = layui.table;
            var tree = layui.tree
                , layer = layui.layer
                , util = layui.util

            var Hob=table.render({
                elem: '#test'
                , url: 'https://localhost:44398/api/DicAPI/Show'
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , title: '用户数据表'
                , cols: [[
                    , { field: 'valID', title: '', width: 35, fixed: 'left', unresize: true, sort: true }
                    , { field: 'keyName', title: '字典类别', width: 120, edit: 'text' }
                    , { field: 'valName', title: '字典值', width: 120, edit: 'text' }
                    , { field: 'firstPY', title: '拼音码', width: 120, edit: 'text' }
                    , { field: 'sort', title: '显示顺序', width: 120 }
                    , { field: 'remark', title: '备注', width: 120, edit: 'text' }
                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150 }
                ]]
                , page: true
            });

            //头工具栏事件
            table.on('toolbar(test)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：' + data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选' : '未全选');
                        break;

                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                //console.log(obj)
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: 'https://localhost:44398/api/DicAPI/Del?id=' + data.valID,
                            type: 'post',
                            success: function (d) {
                                layer.msg("删除成功!", function () {
                                    Hob.reload();
                                })
                            }
                        })
                        layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        type: 2,
                        area: ['700px', '600px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/DictWeb/Fantian?id=' + data.valID,
                    });
                }
            });
            var $ = layui.$, active = {
                reload: function () {
                    var demoReload = $('#demoReload');

                    //执行重载
                    table.reload('test', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {

                            keyName: demoReload.val()

                        }
                    });
                },
                add: function () {
                    layer.open({
                        type: 2,
                        area: ['700px', '600px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/DictWeb/Add'
                    });
                }
            };

            $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            //文档就绪
            $(function () {
                $.ajax({
                    url: "https://localhost:44398/api/DicAPI/Shu",
                    type: "get",
                    success: function (data1) {
                        //常规用法

                        //基本演示
                        tree.render({
                            elem: '#test1'
                            , data: data1
                            , showCheckbox: false //是否显示复选框
                            , id: 'demoId1'
                            , isJump: true //是否允许点击节点时弹出新窗口跳转
                            , click: function (obj) {
                                var data = obj.data;  //获取当前点击的节点数据
                                console.log(data);

                                table.reload('test', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                    , where: {
                                        parentID: data.id
                                    }
                                });
                            }
                        });
                    }
                })
            })
        });
    </script>

</body>
</html>